#include <iostream>

#include <QtGui>

#include "titlescreen_frame.h"
#include "invader_label.h"
#include "invader_menu.h"

TitleScreen::TitleScreen (QWidget *parent)
  : QFrame (parent)
{
  QVBoxLayout *layout = new QVBoxLayout;

  layout->addWidget (new InvaderLabel ("space invaders", this),
                     0, Qt::AlignHCenter);

  QStringList items;
  items << "new game" << "reset highscore" << "exit";
  _menu = new InvaderMenu (items, this);
  _menu->setFocusPolicy (Qt::ClickFocus);
  layout->addWidget (_menu, 0, Qt::AlignHCenter);
  connect (_menu, SIGNAL (itemSelected (unsigned int)),
           this, SLOT (menuItemSelected (unsigned int)));
  connect (_menu, SIGNAL (activity ()), this, SLOT (menuActivity ()));

  QVBoxLayout *helpMsg = new QVBoxLayout;
  helpMsg->addWidget (new InvaderLabel ("left\\right: moves tank", this), 0,
                      Qt::AlignHCenter);
  helpMsg->addWidget (new InvaderLabel ("space: fires bullet", this), 0,
                      Qt::AlignHCenter);
  helpMsg->addWidget (new InvaderLabel ("p: pauses game", this), 0,
                      Qt::AlignHCenter);
  helpMsg->addWidget (new InvaderLabel ("ESC: displays in-game menu", this), 0,
                      Qt::AlignHCenter);

  layout->addLayout (helpMsg);

  setLayout (layout);
}

void TitleScreen::focusInEvent (QFocusEvent *event)
{
  _menu->setFocus ();
}

void TitleScreen::showEvent (QShowEvent *event)
{
  if (!event->spontaneous ())
    emit shown ();
}

void TitleScreen::menuItemSelected (unsigned int item)
{
  switch (item) {
    case 0: emit beginNewGame (); break;
    case 1: emit clearHighscore (); break;
    case 2: emit quit (); break;
    default: break;
  }
}

void TitleScreen::menuActivity ()
{
  emit activity ();
}
